import logging
import util.dtest
import test.utils
import test.neon_support as ns

print '''Test of 2 nodes, syncing some amount of faked data.'''

dtest = util.dtest.DTest()
cond = ns.Conditions(dtest)

node_a = dtest.add_node(ns.NeonNode('A'))
node_b = dtest.add_node(ns.NeonNode('B'))
node_c = dtest.add_node(ns.NeonNode('C'))

# Create link between A <-> B
node_a_in = node_a.reg_tx_tcp_in()
node_b.reg_tx_tcp_out(node_a_in, 1)
node_c.reg_tx_tcp_out(node_a_in, 1)

# setup file
types = '''# type file
def config(atom_store):
    t = AtomType('test')
    t.add_table('test', ('SELECT guid FROM test WHERE i = %s', 'i'))
    atom_store.reg_type(t)
'''

node_a.reg_dynfile('test.types', types)
node_b.reg_dynfile('test.types', types)
node_c.reg_dynfile('test.types', types)

dtest.set_mapper(util.dtest.LocalMapper())

dtest.configure()
dtest.setup_files()

def test_script(tr):
    node_a.run_tx()
    node_b.run_tx()
    node_c.run_tx()
    
    tr.wait_for(5, cond.on_all(cond.tx_is_running))
    tr.wait_for(5, cond.channel_state, 'A', '*', 'B', 'handshake')
    tr.wait_for(5, cond.channel_state, 'A', '*', 'C', 'handshake')
    tr.wait_for(5, cond.channel_state, 'B', '*', 'A', 'handshake')
    tr.wait_for(5, cond.channel_state, 'C', '*', 'A', 'handshake')

    node_a.run_sync(None, {'NEON_TYPE_FILE' : 'test.types',
                           'NEON_DB_TYPE'   : 'mysql',
                           'NEON_DB_USER'   : 'neon',
                           'NEON_DB_HOST'   : 'localhost',
                           'NEON_DB_PASSWD' : '',
                           'NEON_DB_DBNAME'   : 'neon_test_1'})

    node_b.run_sync(None, {'NEON_TYPE_FILE' : 'test.types',
                           'NEON_DB_TYPE'   : 'mysql',
                           'NEON_DB_USER'   : 'neon',
                           'NEON_DB_HOST'   : 'localhost',
                           'NEON_DB_PASSWD' : '',
                           'NEON_DB_DBNAME'   : 'neon_test_2'})

    node_c.run_sync(None, {'NEON_TYPE_FILE' : 'test.types',
                           'NEON_DB_TYPE'   : 'mysql',
                           'NEON_DB_USER'   : 'neon',
                           'NEON_DB_HOST'   : 'localhost',
                           'NEON_DB_PASSWD' : '',
                           'NEON_DB_DBNAME'   : 'neon_test_3'})
    tr.sleep(2)

    q = 'DROP TABLE IF EXISTS test; CREATE TABLE test (guid VARCHAR(10), i INTEGER);'
    node_a.host().run_system('echo "%s" | mysql -u neon neon_test_1' % q)
    node_b.host().run_system('echo "%s" | mysql -u neon neon_test_2' % q)
    node_c.host().run_system('echo "%s" | mysql -u neon neon_test_3' % q)
    tr.pause()

    def ins_fake_entry(n, i):
        q = '''
        INSERT INTO test VALUES ('A:%d', %d);
        INSERT INTO neon_atom_meta VALUES ('A:%d', 'A:%d', 'test', 0, 1);
        ''' % (i, i, i, i)
        n.host().run_system('echo "%s" | mysql -u neon neon_test_1' % q)

    for i in xrange(5):
	ins_fake_entry(node_a, i)
    tr.pause()
        
dtest.run(test_script)
